package it.hive.controller;

import it.hive.controller.download.DownloadGUI;
import it.hive.controller.renderer.CustomListCell;
import it.hive.mediaplayer.MediaPlayerController;
import it.hive.mediaplayer.beans.impl.Traccia;
import it.hive.model.Application;
import it.hive.model.common.ApplicationProperties;
import it.hive.model.common.constants.M2O_Default;
import it.hive.model.domain.media.Gruppo;
import it.hive.model.domain.media.Trasmissione;

import java.net.URL;
import java.util.ResourceBundle;

import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Label;
import javafx.scene.control.ListView;
import javafx.scene.control.MenuItem;
import javafx.scene.control.SplitPane;
import javafx.scene.effect.DropShadow;

public class MediaController implements Initializable {
	@FXML
	private static ListView<Gruppo> listaGruppi;
	@FXML
	private static ListView<Trasmissione> listaTrasmissioni;
	@FXML
	private static ListView<Traccia> listaTracce;
	@FXML
	private static Label label;
	@FXML
	private MenuItem downloadMenuItem;
	@FXML
	private MenuItem playMenuItem;
	@FXML
	private ComboBox<String> autori;
	@FXML
	private SplitPane left;

	private Application application = Application.getIstanza();
	private Gruppo gruppoSelezionato;
	private CustomListCell<?> customListCell;

	@SuppressWarnings("unchecked")
	@Override
	public void initialize(URL arg0, ResourceBundle arg1) {
		customListCell = new CustomListCell<>();
		listaGruppi.setItems(ApplicationProperties.observableGruppi);
		listaTrasmissioni.setItems(ApplicationProperties.observableTrasmissioni);
		listaTracce.setItems(ApplicationProperties.observableTrackList);
		listaGruppi.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<Gruppo>() {

			@Override
			public void changed(ObservableValue<? extends Gruppo> observable, Gruppo oldValue, Gruppo newValue) {
				if (newValue != null) {
					listaTracce.getItems().clear();
					listaTrasmissioni.getItems().clear();
					gruppoSelezionato = newValue;
					application.recuperaTrasmissioni(newValue);
				}
			}
		});

		listaTrasmissioni.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<Trasmissione>() {

			@Override
			public void changed(ObservableValue<? extends Trasmissione> observable, Trasmissione oldValue, Trasmissione newValue) {
				if (newValue != null) {
					application.recuperaTrackList(gruppoSelezionato, newValue);
				}
			}
		});
		listaGruppi.setCellFactory(((CustomListCell<Gruppo>) customListCell).getCellFactory());
		listaTrasmissioni.setCellFactory(((CustomListCell<Trasmissione>) customListCell).getCellFactory());
		listaTracce.setCellFactory(((CustomListCell<Traccia>) customListCell).getCellFactory());
		listaTracce.getSelectionModel().selectedItemProperty().addListener(application.getTrackListener());
		playMenuItem.disableProperty().bind(ApplicationProperties.currentSelectedTrack.isNull());
		downloadMenuItem.visibleProperty().bind(
				ApplicationProperties.currentSelectedTrack.isNotNull().and(ApplicationProperties.downloadInCorso.not()));

		DropShadow destra = (DropShadow) listaTracce.effectProperty().getValue();
		DropShadow sinistra = (DropShadow) left.effectProperty().getValue();

		destra.radiusProperty().bind(ApplicationProperties.rightAudio);
		sinistra.radiusProperty().bind(ApplicationProperties.leftAudio);

		left.setEffect(sinistra);
		listaTracce.setEffect(destra);

	}

	public static void selectNext() {
		if (listaTracce != null && !listaTracce.getItems().isEmpty()) {
			listaTracce.getSelectionModel().selectNext();
		}
	}

	public static void selectPrevious() {
		if (listaTracce != null && !listaTracce.getItems().isEmpty()) {
			listaTracce.getSelectionModel().selectPrevious();
		}
	}

	@FXML
	private void play(ActionEvent e) {
		Traccia traccia = ApplicationProperties.currentSelectedTrack.get();
		if (traccia != null) {
			if (traccia.isUnparsed()) {
				application.risolviURL(traccia);
			}
			MediaPlayerController.getMediaPlayerController().play(traccia);
		}
	}

	@FXML
	private void download(ActionEvent e) {
		Traccia traccia = ApplicationProperties.currentSelectedTrack.get();
		if (M2O_Default.TRACCIA_STREAMING.equals(traccia)) {
			ApplicationProperties.downloadInCorso.set(true);
			return;
		}
		DownloadGUI.scaricaTraccia(traccia);
	}

}
